package com.chuangke.sinotrukbudget.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.chuangke.core.http.HttpResult;
import com.chuangke.sinotrukbudget.model.BudgetControlAccount;
import com.chuangke.sinotrukbudget.model.BudgetControlAccountDept;
import com.chuangke.sinotrukbudget.service.BudgetControlAccountDeptService;
import com.chuangke.sinotrukbudget.service.BudgetControlAccountService;

/**
 * @author chuangke
 * @date 2022-10-20
 */
@RestController
@RequestMapping("budget/control")
public class BudgetControlController {

	@Autowired
	private BudgetControlAccountService budgetControlAccountService;
	@Autowired
	private BudgetControlAccountDeptService budgetControlAccountDeptService ;

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/saveca")
	public HttpResult saveCa(@RequestBody List<BudgetControlAccount> records) {
		budgetControlAccountService.save(records);
		return HttpResult.ok();
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/deleteca")
	public HttpResult deleteCa(@RequestBody List<BudgetControlAccount> records) {
		budgetControlAccountService.delete(records);
		return HttpResult.ok();
	}
	
	@SuppressWarnings("unchecked")
	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/savecad")
	public HttpResult saveCad(@RequestBody Map<String,Object> params) {
		String deptId = (String)params.get("deptId") ;
		List<String> caIdList = (List<String>)params.get("caIdList") ;
		List<String> cadIdList = (List<String>)params.get("cadIdList") ;
		String controlMode = (String) params.get("controlMode") ;
		
		
		List<BudgetControlAccountDept> records = new ArrayList<>() ;
		BudgetControlAccountDept bcad = null ;
		for(String caId : caIdList) {
			for(String cadId : cadIdList) {
				bcad = new BudgetControlAccountDept() ;
				bcad.setDeptId(deptId) ;
				bcad.setControlAccountId(caId) ;
				bcad.setControlDeptId(cadId) ;
				bcad.setControlMode(controlMode) ;
				records.add(bcad) ;
			}
		}
		
		budgetControlAccountDeptService.save(records);
		return HttpResult.ok();
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/savecad1")
	public HttpResult saveCad1(@RequestBody List<BudgetControlAccountDept> records) {
		budgetControlAccountDeptService.save(records);
		return HttpResult.ok();
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/deletecad")
	public HttpResult deleteCad(@RequestBody List<BudgetControlAccountDept> records) {
		budgetControlAccountDeptService.delete(records);
		return HttpResult.ok();
	}

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/listca")
	public HttpResult listCa(@RequestBody Map<String,String> params) {
		String deptId = params.get("deptId") ;
		return HttpResult.ok(budgetControlAccountService.findList(deptId));
	}
	
	@SuppressWarnings("unchecked")
	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/listcad")
	public HttpResult listCad(@RequestBody Map<String,Object> params) {
		String deptId = (String)params.get("deptId") ;
		List<String> accountIdList = (List<String>)params.get("accountIdList") ;
		return HttpResult.ok(budgetControlAccountDeptService.findList(deptId,accountIdList));
	}
}
